import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;

import javax.swing.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.sql.Date;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Locale;

/**
 * @author songx
 * @date 2019/7/3 23:12
 */
public class MouseClickSureEnter implements MouseListener {

    private static final String SPACE = "\n";
    private static final String NULL_STRING = "  ";

    public void mouseClicked(MouseEvent e) {
        String inPutTime = Main.inPutTimeArea.getText().trim();
        if (StrUtil.isBlank(inPutTime)) {
            JOptionPane.showMessageDialog(null, "请输入当前打卡时间！");
            return;
        }
        DateTime parse;
        try {
            parse = DateUtil.parse(inPutTime);
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "请按格式输入时间！", "格式: 09:00", JOptionPane.WARNING_MESSAGE);
            return;
        }
        if (ObjectUtil.isEmpty(parse)) {
            JOptionPane.showMessageDialog(null, "请按格式输入时间！", "格式: 09:00", JOptionPane.WARNING_MESSAGE);
            return;
        }
        final Timestamp now = new Timestamp(System.currentTimeMillis());

        String currentPickTime = DateUtil.formatDateTime(parse);
        String today = currentPickTime.split(" ")[0];

        String overTime = today + " 10:00:00";
        String inClassTime = today + " 09:00:00";
        String outOfClassTime = today + " 18:00:00";

        DateTime overDay = DateUtil.parse(overTime);
        DateTime ShouldInClassDate = DateUtil.parse(inClassTime);
        DateTime standardOutClassDate = DateUtil.parse(outOfClassTime);
        DateTime actualInClassDate = DateUtil.parse(currentPickTime);
        if (overDay.before(actualInClassDate)) {
            JOptionPane.showMessageDialog(null, "你已错过打卡时间，请申请补卡！", "旷工！", JOptionPane.WARNING_MESSAGE);
            return;
        }
        Long diffTimeInClass = 0L;
        if (actualInClassDate.after(ShouldInClassDate)) {
            diffTimeInClass = DateUtil.between(ShouldInClassDate, actualInClassDate, DateUnit.MINUTE);
        }
        DateTime shouldBeOutClassDate = DateUtil.offset(standardOutClassDate, DateField.MINUTE, diffTimeInClass.intValue());
        Long diffTimeOutClassHour = DateUtil.between(shouldBeOutClassDate, DateTime.now(), DateUnit.HOUR);
        Long diffTimeOutClassMin = DateUtil.between(shouldBeOutClassDate, DateTime.now(), DateUnit.MINUTE);
        DateTime overtimeInClass = DateUtil.offsetMinute(shouldBeOutClassDate, 30);
        Long diffTimeOverTime = DateUtil.between(DateTime.now(), overtimeInClass, DateUnit.MINUTE);
        long hasHadOverTime = 0L;
        if (System.currentTimeMillis() > overtimeInClass.getTime()) {
            hasHadOverTime = DateUtil.between(DateTime.now(), overtimeInClass, DateUnit.MINUTE);
        }
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("①:");
        stringBuilder.append(String.format("今天打卡时间为:%s", actualInClassDate));
        stringBuilder.append(NULL_STRING);
        stringBuilder.append(SPACE);
        stringBuilder.append("②:");
        stringBuilder.append(String.format("本公司当前标准下班时间为：%s", standardOutClassDate));
        stringBuilder.append(SPACE);
        stringBuilder.append("③:");
        stringBuilder.append(String.format("当前系统时间为：%s", DateUtil.now()));
        try {
            stringBuilder.append(String.format("; 网络时间为：%s", null == DateUtil.formatTime(DateUtil.parse(getWebsiteDatetime())) ? "..૭٩(●´৺`●)و ..没有网.." : DateUtil.formatTime(DateUtil.parse(getWebsiteDatetime()))));
        } catch (Exception w) {
            w.printStackTrace();
        }
        stringBuilder.append(SPACE);
        stringBuilder.append("-----------------------------------------------------------------------------");
        stringBuilder.append(SPACE);
        stringBuilder.append("④:");
        if (diffTimeInClass.equals(0L)) {
            stringBuilder.append("今天可在标准时间点下班; ");
        } else {
            stringBuilder.append(String.format("今天需要后于标准时间%s分钟下班;", diffTimeInClass));
        }
        stringBuilder.append(String.format("即下班时间为：%s", shouldBeOutClassDate));
        stringBuilder.append(SPACE);
        stringBuilder.append("-----------------------------------------------------------------------------");
        stringBuilder.append(SPACE);
        stringBuilder.append("⑤:");
        if (now.getTime() <= shouldBeOutClassDate.getTime()) {
            stringBuilder.append(String.format("再过%s小时即可下班;即还有%s分钟;", diffTimeOutClassHour, diffTimeOutClassMin));
        }
        if (shouldBeOutClassDate.getTime() <= now.getTime() && now.getTime() < overtimeInClass.getTime()) {
            stringBuilder.append("你现在已经处于下班状态！");
        }
        if (now.getTime() > overtimeInClass.getTime()) {
            stringBuilder.append("你现在已经处于加班状态！");
        }
        stringBuilder.append(SPACE);
        stringBuilder.append("-----------------------------------------------------------------------------");
        stringBuilder.append(SPACE);
        stringBuilder.append("⑥:");
        stringBuilder.append(String.format("加班开始时间为：%s;", overtimeInClass));
        if (new Timestamp(System.currentTimeMillis()).getTime() <= overtimeInClass.getTime()) {
            stringBuilder.append(String.format("即%s分钟后才开始算加班！", diffTimeOverTime));
        } else {
            stringBuilder.append(String.format("现在已经加班%s分钟", diffTimeOverTime));
        }
        stringBuilder.append(SPACE);
        stringBuilder.append("-----------------------------------------------------------------------------");
        stringBuilder.append(SPACE);
        Double effectiveHour = Math.floor(new Double(hasHadOverTime / 60));
        stringBuilder.append(String.format("⑦: 如果你没有打下班卡;那么当前有效加班为%s分钟;即有效时长为：%s小时", hasHadOverTime, effectiveHour));
        stringBuilder.append(SPACE);
        Long offset = (effectiveHour.intValue() + 1) * 60 - hasHadOverTime;
        stringBuilder.append("-----------------------------------------------------------------------------");
        stringBuilder.append(SPACE);
        stringBuilder.append(String.format("⑧: 当前时偏移剩余时间为：%s分钟;", offset));
        if (offset > 30) {
            stringBuilder.append(" 建议越早下班越好！");
        } else {
            stringBuilder.append(String.format(" 建议%s分钟后下班效益最大！", offset));
        }
        Main.outPutContentArea.setText(stringBuilder.toString());
    }


    private static String getWebsiteDatetime() {
        try {
            URL url = new URL("http://www.taobao.com");
            URLConnection uc = url.openConnection();
            uc.connect();
            long ld = uc.getDate();
            Date date = new Date(ld);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
            return sdf.format(date);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void mousePressed(MouseEvent e) {

    }

    public void mouseReleased(MouseEvent e) {

    }

    public void mouseEntered(MouseEvent e) {

    }

    public void mouseExited(MouseEvent e) {

    }
}
